#include "demomenu.h"
#include "QDebug"
#include <QGraphicsEllipseItem>
#include <QLayout>
#include <enellipseitem.h>

DemoMenu::DemoMenu(  QWidget *parent, EnDrawContext* d ) :
    QWidget( parent )
{
    drawContext = d;
    setWindowTitle( "Demo Menu " );
    setGeometry( QRect( 900, 300, 380, 360 ) );
    setWindowFlags( Qt::WindowStaysOnTopHint );

    // Setup Drop down menu for background image
    comboBox.setParent( this );
    comboBox.setGeometry( QRect( 10, 10, 330, 25 ) );
    comboBox.addItem( "Clouds", ":/images/clouds.jpg" );
    comboBox.addItem( "Lava", ":/images/lava.jpg" );
    comboBox.addItem( "Nebula", ":/images/nebula.jpg" );

    QObject::connect( &comboBox, SIGNAL( activated( int ) ), this, SLOT( changeBackground( int ) ) );

    // Red
    redLabel.setParent( this );
    redLabel.setGeometry( QRect( 10, 50, 50, 25 ) );
    redLabel.setText( "Red" );

    redSlider.setParent( this );
    redSlider.setOrientation( Qt::Horizontal );
    redSlider.setGeometry( QRect( 10, 80, 330, 25 ) );
    redSlider.setRange( 0, 255 );

    QObject::connect( &redSlider, SIGNAL( valueChanged( int ) ), this, SLOT( setObjectRed(int) ) );

    // Green
    greenLabel.setParent( this );
    greenLabel.setGeometry( QRect( 10, 110, 50, 25 ) );
    greenLabel.setText( "Green" );

    greenSlider.setParent( this );
    greenSlider.setOrientation( Qt::Horizontal );
    greenSlider.setGeometry( QRect( 10, 140, 330, 25 ) );
    greenSlider.setRange( 0, 255 );

    QObject::connect( &greenSlider, SIGNAL( valueChanged( int ) ), this, SLOT( setObjectGreen(int) ) );

    // Blue
    blueLabel.setParent( this );
    blueLabel.setGeometry( QRect( 10, 170, 50, 25 ) );
    blueLabel.setText( "Blue" );

    blueSlider.setParent( this );
    blueSlider.setOrientation( Qt::Horizontal );
    blueSlider.setGeometry( QRect( 10, 200, 330, 25 ) );
    blueSlider.setRange( 0, 255 );

    QObject::connect( &blueSlider, SIGNAL( valueChanged( int ) ), this, SLOT( setObjectBlue(int) ) );

    // Alpha
    alphaLabel.setParent( this );
    alphaLabel.setGeometry( QRect( 10, 230, 50, 25 ) );
    alphaLabel.setText( "Alpha" );


    alphaSlider.setParent( this );
    alphaSlider.setOrientation( Qt::Horizontal );
    alphaSlider.setGeometry( QRect( 10, 260, 330, 25 ) );
    alphaSlider.setRange( 0, 255 );

    QObject::connect( &alphaSlider, SIGNAL( valueChanged(int) ), this, SLOT( setObjectAlpha(int) ) );

    // Setup button for spawning ellipses
    ellipseButton.setText( "Spawn 10 Ellipses" );
    ellipseButton.setParent( this );
    ellipseButton.setGeometry( QRect( 67, 300, 250, 25 ) );

    numEllipseLabel.setText( "Ellipses on Screen: 0 " );
    numEllipseLabel.setParent( this );
    numEllipseLabel.setGeometry( 128, 330, 300, 25 );

    QObject::connect( &ellipseButton, SIGNAL( released() ), this, SLOT( spawnEllipse() ) );

    // Connect the Scene update to the slider values so we get updated values when selected new ellipses
    QObject::connect( drawContext->getView()->scene(), SIGNAL( selectionChanged() ), this, SLOT( updateSliders() ) );

    show();
}

// Slot that changes the module and engine background to the comboBox selection
void DemoMenu::changeBackground( int index )
{
    //qDebug() << comboBox.itemData( index ).toString() << endl;
    drawContext->getModule()->setBackground( comboBox.itemData( index ).toString() );
    drawContext->getView()->scene()->setBackgroundBrush( *drawContext->getModule()->getBackgroundBrush() );
}

// Slot that spawns an ellipse in the EnGraphicsView
void DemoMenu::spawnEllipse()
{
    for( int i = 0; i < 10; i++ )
    {
        drawContext->getModule()->localAddBody();
    }

    numEllipseLabel.setText( "Ellipses on Screen: " + QString::number( drawContext->getModule()->bodyMap.size() ) );
}

void DemoMenu::setObjectRed( int value )
{
    for( int i = 0; i < drawContext->getView()->scene()->selectedItems().size(); i++ )
    {
        EnEllipseItem* p = qgraphicsitem_cast<EnEllipseItem*>( drawContext->getView()->scene()->selectedItems().at( i ) );
        currentColor = p->brush().color().toRgb();
        currentColor.setRed( value );
        p->setSyncedBrush( currentColor );
    }

}

void DemoMenu::setObjectGreen( int value )
{
    for( int i = 0; i < drawContext->getView()->scene()->selectedItems().size(); i++ )
    {
        EnEllipseItem* p = qgraphicsitem_cast<EnEllipseItem*>( drawContext->getView()->scene()->selectedItems().at( i ) );
        currentColor = p->brush().color().toRgb();
        currentColor.setGreen( value );
        p->setSyncedBrush( currentColor );
    }
}

void DemoMenu::setObjectBlue( int value )
{
    for( int i = 0; i < drawContext->getView()->scene()->selectedItems().size(); i++ )
    {
        EnEllipseItem* p = qgraphicsitem_cast<EnEllipseItem*>( drawContext->getView()->scene()->selectedItems().at( i ) );
        currentColor = p->brush().color().toRgb();
        currentColor.setBlue( value );
        p->setSyncedBrush( currentColor );
    }
}

void DemoMenu::setObjectAlpha( int value )
{
    for( int i = 0; i < drawContext->getView()->scene()->selectedItems().size(); i++ )
    {
        EnEllipseItem* p = qgraphicsitem_cast<EnEllipseItem*>( drawContext->getView()->scene()->selectedItems().at( i ) );
        currentColor = p->brush().color().toRgb();
        currentColor.setAlpha( value );
        p->setSyncedBrush( currentColor );
    }
}

void DemoMenu::updateSliders()
{
    if( drawContext->getView()->scene()->selectedItems().size() != 0 )
    {
        QGraphicsEllipseItem* p = qgraphicsitem_cast<QGraphicsEllipseItem*>( drawContext->getView()->scene()->selectedItems().at( 0 ) );
        currentColor = p->brush().color().toRgb();

        // Change value of sliders to the current ellipse without triggering the update signal
        redSlider.blockSignals( true );
        redSlider.setValue( currentColor.red() );
        redSlider.blockSignals( false );

        greenSlider.blockSignals( true );
        greenSlider.setValue( currentColor.green() );
        greenSlider.blockSignals( false );

        blueSlider.blockSignals( true );
        blueSlider.setValue( currentColor.blue() );
        blueSlider.blockSignals( false );

        alphaSlider.blockSignals( true );
        alphaSlider.setValue( currentColor.alpha() );
        alphaSlider.blockSignals( false );
    }
}
